package com.sumainfor.pc.market.service;

import com.sumainfor.common.Constants;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.ComUtils;
import com.sumainfor.common.utlis.FileSubufferUtils;
import com.sumainfor.common.utlis.ParamsCommon;
import com.sumainfor.pc.market.dao.TcmktCorporateMapper;
import com.sumainfor.pc.market.dao.UtilsMapper;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import java.io.Serializable;
import java.sql.SQLOutput;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

/**
 * <p>
 * 企业客户
 * <p>
 *
 * @author: zhoucong
 * @create: 2019-05-29 19:51
 **/
@Service
public class TcmktCorporateService implements Serializable {

    private ReentrantLock lock = new ReentrantLock(false);

    Logger log = LoggerFactory.getLogger(TcmktCorporateService.class);// 日志

    @Autowired
    TcmktCorporateMapper tcmktCorporateMapper;

    @Autowired
    HousingInfoEvaService housingInfoEvaService;

    @Autowired
    TcmktCorpHousService tcmktCorpHousService;

    @Autowired
    UtilsMapper utilsMapper;


    /**
    * <p>
    * 增加企业客户
    * </p>
    *
    * @author: zhoucong
    * @since:
    */
    public JsonResult addCorporate(Map<String,Object> params){

        JsonResult result = new JsonResult();
        /**
         * 加锁操作
         */
        lock.lock();
        try {
            log.info("增加操作");
            params.put("term", "2");//创建端  1 租客端 2市场部 3 工程部 4运营部 5财务部 6总经办 7发展部
            params.put("fnc",
                    this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
            ParamsCommon.commonInsertParms(params);
            System.out.println("sssssssss"+params);
            //营业执照
           List<Map<String, Object>> BlicenseList = (List<Map<String, Object>>) params.get("Blicense");
           String Blicense = FileSubufferUtils.ListFlieStringUtils(BlicenseList);
          params.put("Blicense",Blicense);//图片

            //合同附件
            List<Map<String, Object>> FileAnnexList = (List<Map<String, Object>>) params.get("FileAnnex");
           String FileAnnex = FileSubufferUtils.ListTranStringUtils(FileAnnexList);
            params.put("FileAnnex",FileAnnex);


            String UUID;
            Map<String,Object>uuid=utilsMapper.getUUID(new HashMap<String,Object>(){{
                put("uuid", "CorId");
                put("database", Constants.DATA_SOURCE_TCMKT);
                put("surface", "tcmkt_corporate");
            }});
            if(uuid!=null){
                int number=Integer.valueOf(uuid.get("uuid").toString());
                UUID= ComUtils.getLocalTrmSeqNum("co",number);
            }else{
                UUID=ComUtils.getLocalTrmSeqNum("co",0);
            }
            params.put("CorId",UUID);
            System.out.println("保存传进来的参数"+params);
           Integer cout = tcmktCorporateMapper.addCorporate(params);
            System.out.println("保存是否成功"+cout);
            if(cout<=0){
                result.putFailed("保存失败!");
            }else{
               //增加客户与房源中间表
                List<Map<String,Object>> HouInfoIdList = (List<Map<String, Object>>) params.get("HouInfoIdList");

                for (Map<String,Object> HouInfoIdMap:HouInfoIdList) {
                    System.out.println("每次进行添加中间表"+HouInfoIdMap);
                    HouInfoIdMap.put("CorId", MapUtils.getString(params,"CorId"));
                    Integer coutMap =MapUtils.getInteger(HouInfoIdMap,"IfOperaType");

                    Integer a = tcmktCorporateMapper.getCorporateHousCount(HouInfoIdMap);
                    System.out.println("查询合租是否在中间表"+a);
                    if (a==0){
                        tcmktCorpHousService.addCorporateHous(HouInfoIdMap);
                    }




//
//
//                    if (coutMap==0){ //合租
//                        Integer a = tcmktCorporateMapper.getCorporateHousCount(HouInfoIdMap);
//                        System.out.println("查询合租是否在中间表"+a);
//                        if (a==0){
//                            tcmktCorpHousService.addCorporateHous(HouInfoIdMap);
//                        }
//                    }if(coutMap==1){//整租
//                  List<Map<String,Object>> ListOne = housingInfoEvaService.getHouInfoId(HouInfoIdMap);
//                        System.out.println("如果是整租查询房源详表里面房源编号相同的"+ListOne);
//                        if(ListOne.size()==1){
//                            for (Map<String,Object> ListMap:ListOne) {
//                                ListMap.put("CorId", MapUtils.getString(params, "CorId"));
//                                Integer a = tcmktCorporateMapper.getCorporateHousCount(ListMap);
//                                System.out.println("每次添加之前先进行查询" + a);
//                                if (a == 0) {
//                                    tcmktCorpHousService.addCorporateHous(ListMap);
//                                }
//                            }
//                        }if (ListOne.size()>1) {
//                            for (Map<String, Object> ListMap : ListOne) {
//                                if (!ListMap.get("HouInfoId").equals(HouInfoIdMap.get("HouInfoId"))) {
//                                    ListMap.put("CorId", MapUtils.getString(params, "CorId"));
//                                    System.out.println("进行添加的房源中间表" + ListMap);
//                                    Integer a = tcmktCorporateMapper.getCorporateHousCount(ListMap);
//                                    System.out.println("每次添加之前先进行查询" + a);
//                                    if (a == 0) {
//                                        tcmktCorpHousService.addCorporateHous(ListMap);
//                                    }
//                                }
//                            }
//                        }
//                    }

                    //修改房源详表
                    if (MapUtils.getInteger(HouInfoIdMap,"IfOperaType")==0){//合租
                        housingInfoEvaService.updateHouInfoState(HouInfoIdMap);

                    }else{
                     Map<String,Object> zhengzuMap = new HashMap<>();
                     zhengzuMap.put("HouId",HouInfoIdMap.get("HouId"));
                    List<Map<String,Object>> updateList = housingInfoEvaService.getHouInfoId(zhengzuMap);
                    for (Map<String,Object> updateMap:updateList) {
                        updateMap.put("CorId", MapUtils.getString(params, "CorId"));
                        housingInfoEvaService.updateHouInfoState(HouInfoIdMap);
                    }
                    }
                 }
                result.putSuccess("保存成功!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//事物回滚
            result.putFailed("服务器繁忙...");
        } finally {
            lock.unlock();
        }
        return result;
    }



    public List<Map<String,Object>> getCorporateList(Map<String,Object> params){
        List<Map<String,Object>> mapList = tcmktCorporateMapper.getCorporateList(params);
        for (Map<String,Object> map1:mapList){

            int  HouNum =0; //已租房间
            int TenRoomMax=0;//最多居住人数
            int TenRoom = 0;//已居住人数
            List<Map<String,Object>> getCorporateHouList = tcmktCorporateMapper.getCorporateHouList(map1);
            for (Map<String,Object> map2:getCorporateHouList){
                if (MapUtils.getInteger(map2,"IfOperaType")==1){//整租
                    Integer cout  = tcmktCorporateMapper.getHouNum(map2)-1;
                    HouNum = HouNum + cout;
                }else{
                    HouNum = HouNum +1;
                }
                if (MapUtils.getInteger(map2,"Status")==1){
                    TenRoom =TenRoom + tcmktCorporateMapper.getCorporatemember(map2);
                }
            }
            TenRoomMax = HouNum*2;
            map1.put("HouNum",HouNum);
            map1.put("TenRoomMax",TenRoomMax);
            map1.put("TenRoom",TenRoom);
        }

                return mapList;
            }

    public Integer getCorporateCount(Map<String,Object> params){

        return tcmktCorporateMapper.getCorporateCount(params);
    }




    public List<Map<String, Object>> getCorHouList(Map<String,Object> params){
        List<Map<String,Object>> maplist = tcmktCorporateMapper.getCorHouList(params);
        for (Map<String,Object> map1:maplist){

        int HouSum = 0; //最多入住
        int HouRoom = 0; //已入住


            if(MapUtils.getInteger(map1,"IfOperaType")==1){//表示整租
                HouSum= (tcmktCorporateMapper.getHouNum(map1)-1)*2;
            }else{//表示合租
                HouSum = 2;
            }
            HouRoom = tcmktCorporateMapper.getCorporatemember(map1);
            map1.put("HouSum",HouSum);
            map1.put("HouRoom",HouRoom);
        }
        return maplist;
    }

    public Integer getCorHouCount(Map<String,Object> params){

        return tcmktCorporateMapper.getCorHouCount(params);
    }






}
